/**
 * 
 */
package com.sandberg.shopper;

import com.sandberg.shopper.core.HelpScreen;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CheckBox;

/**
 * @author Jesper
 *
 */
public class ShopperSettingsActivity extends Activity {
	
	private final int DIALOG_HELP = 1;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings);

        // Restore preferences
        SharedPreferences settings = getSharedPreferences(Property.SHARED_PREFS_NAME, 0);

        // Get storeFields value
        final CheckBox checkBox = (CheckBox) findViewById(R.id.checkBoxStoreFields);
        boolean storeFields = settings.getBoolean(Property.SHARED_STORE_FIELDS, true);
        checkBox.setChecked(storeFields);
    }

    @Override
    protected void onStop() {
    	super.onStop();
    	// We need an Editor object to make preference changes.
    	SharedPreferences settings = getSharedPreferences(Property.SHARED_PREFS_NAME, 0);
    	SharedPreferences.Editor editor = settings.edit();

    	// Set storeFields value
        final CheckBox checkBox = (CheckBox) findViewById(R.id.checkBoxStoreFields);
        boolean storeFields = checkBox.isChecked();

    	editor.putBoolean(Property.SHARED_STORE_FIELDS, storeFields);
    	// Commit the edits!
    	editor.commit();
    }

    /* (non-Javadoc)
	 * @see android.app.Activity#onCreateDialog(int)
	 */
	@Override
	protected Dialog onCreateDialog(int id) {
	    Dialog dialog = null;
	    switch(id) {
	    	case DIALOG_HELP:
	    		dialog = HelpScreen.createHelpScreen(this, R.string.help_settings);
	    		break;
	    }
	    return dialog;
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		
		menu.add(getResources().getString(R.string.option_menu_about))
			.setIcon(android.R.drawable.ic_menu_info_details)
			.setIntent(new Intent(this, ShopperAboutActivity.class));

		menu.add(getResources().getString(R.string.option_menu_help))
			.setIcon(android.R.drawable.ic_menu_help);

		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		String title = (String) item.getTitle();
		if (title.compareTo(getResources().getString(R.string.option_menu_help)) == 0) {
			showDialog(DIALOG_HELP);
			return true;
		}
		return false;
	}
}
